<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/12/26 11:49
 * Desc: 单套餐预订
 */
class Service_Api_Ferry_Booking_Single
{
    private $ferry;

    public function __construct($ferry)
    {
        $this->ferry = $ferry;
    }

    /**
     * 单套餐预订指定套餐
     * @param $ticket_info
     * @param $params array start_date:Y-m-d end_date:Y-m-d
     * @return array
     */
    public function get_price_calendar($params, $ticket_info)
    {
        $ticket_id = $ticket_info['id'];
        $ferry_id   = $ticket_info['ferryid'];

        // 处理提前预订时间
        $before_time = Model_Ferry_Ticket::get_before_time($ticket_info);

        if ($before_time && strtotime($params['start_date']) < $before_time) {
            $params['start_date'] = date("Y-m-d H:i:s", $before_time);
        }

        return Model_Ferry_Ticket_Price::get_price_calendar($ferry_id, array('ticket_id' => $ticket_id), $params);
    }

    /**
     * 单套餐预订获取套餐金额信息
     * @param $params array start_date:Y-m-d;end_date:Y-m-d;member_card_id: 会员卡id
     * @param $ticket_info
     * @return array
     */
    public function get_book_suit($params, $ticket_info)
    {
        $ticket_id      = $ticket_info['id'];
        $ferry_id        = $this->ferry['id'];
        $date           = Arr::get($params, 'date');
        $period_time_id = Arr::get($params, 'period_time_id', ''); // 指定时段id

        $data = array(
            'prices' => array() // 价格列表：多规格或多套餐需要返回
        );

        $sku_params = array('ticket_id' => $ticket_id);
        $skus       = Model_Ferry_Ticket_Sku::get_price_sku($ferry_id, $sku_params);
        if (!empty($skus)) {

            $stock_service = new Service_Ferry_Ticket_Stock();

            $prices = array();

            // 按规格报价
            foreach ($skus as $key => $sku) {
                $ticket_id = $sku['ticket_id'];

                if ($period_time_id && $sku['period_time_id'] != $period_time_id) {
                    continue;
                }

                // 查询报价
                $price_params            = $sku;
                $price_params['date']    = $date;
                $price_params['is_open'] = 1;
                $price                   = Model_Ferry_Ticket_Price::get_price($price_params);
                if (empty($price)) {
                    unset($skus[$key]);
                    continue;
                }

                // 报价处理
                $stock    = $price['number'];
                $prices[] = array(
                    'name' => $ticket_info['title'],
                    'price' => floatval(Currency_Tool::price($price['adultprice'])),
                    'stock' => $stock,
                    'min' => $ticket_info['at_least'],
                    'max' => $stock_service->get_stock_limit($ticket_info, $stock, $date, $params),
                    'ticket_id' => $ticket_id,
                    'sku_id' => $sku['sku_id'],
                    'ticket_info' => $ticket_info
                );
            }

            $data['prices'] = $prices;
        }

        return $data;
    }
}